------------
--HEAL BOT--
------------

if not UnitRosterCheck then
	function UnitRosterCheck(unit)
		if UnitInRange(unit)
				and UnitCanAssist("player", unit)
				and not UnitIsDeadOrGhost(unit)
				and not LineOfSight(unit)
				--Amber-Shaper Un'sok (Parasitic Growth)
				and not UnitDebuff(unit, convert(121949))
				--Twin Consorts (Beast of Nightmares)
				and not UnitDebuff(unit, convert(137341)) then
			return true
		end
	end
end

roster = {
	{
		unit = "player",
		hp = UnitHP("player"),
		role = UnitGroupRolesAssigned("player")
	}
}

local NumGroupMembers = IsInRaid() and GetNumGroupMembers()
		or IsInGroup() and GetNumSubgroupMembers()
		or 0
for i = 1, NumGroupMembers do
	--PLAYERS
	local unit = IsInRaid() and "raid" .. i
			or "party" .. i
	if UnitRosterCheck(unit) then
		tinsert(roster, {
			unit = unit,
			hp = UnitHP(unit),
			role = UnitGroupRolesAssigned(unit)
		})
	end
	--PLAYER'S PETS
	local pet = IsInRaid() and "raid" .. i .. "-pet"
			or "party" .. i .. "-pet"
	if UnitRosterCheck(pet) then
		tinsert(roster, {
			unit = pet,
			hp = UnitHP(pet) * 2,
			role = "PET"
		})
	end
end

if IsInRaid() then
	tremove(roster, 1)
end

--Tsulong (Bathed in Light)
if UnitDebuff("player", convert(122858)) then
	tinsert(roster, { unit = "boss1", hp = 5, role = "BOSS" })
end

table.sort(roster, function(x, y) return x.hp < y.hp end)

if not HealTarget then
	function HealTarget()
		for i = 1, #roster do
			local unit, hp, role = roster[i].unit, roster[i].hp, roster[i].role
			if UnitIsUnit(unit, "player") and hp <= 20 then
				return unit, hp, role
			elseif role == "TANK" and hp <= 25 then
				return unit, hp, role
			elseif role == "HEALER" and hp <= 15 then
				return unit, hp, role
			end
		end
		return roster[1].unit, roster[1].hp, roster[1].role
	end
else
	local Color = {
		["HUNTER"] = "abd473",
		["WARLOCK"] = "9482c9",
		["PRIEST"] = "ffffff",
		["PALADIN"] = "f58cba",
		["MAGE"] = "69ccf0",
		["ROGUE"] = "fff569",
		["DRUID"] = "ff7d0a",
		["SHAMAN"] = "0070de",
		["WARRIOR"] = "c79c6e",
		["DEATHKNIGHT"] = "c41f3b",
		["MONK"] = "00ff96",
	}
	local unit = UnitName(HealTarget()) or "player"
	local _, class = UnitClass(unit)
	PQR_Event("PQR_Text", unit, 3, Color[class])
end